    //
//  PlayerViewController.m
//  ICE_Player
//
//  Created by xl on 11-10-12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PlayerViewController.h"
#include "ICE_global_var.h"
#include "file_list.h"
#import "Player_Model.h"
#import "BGView.h"
#import "Globalize.h"

@interface PlayerViewController(PrivateMethod)
- (void)On_Refresh_Data;
- (void)Ensure_File_Len;
- (void)On_update_Time_Pos_Controls;
- (void)On_Stop_Control_Hide_Timer;
- (void)On_Fire_Control_Hide_Timer;
- (void)On_Do_Hide_Control;
- (void)On_Call_Msg_Box:(ICE_String*)title msg:(ICE_String*)content should_leave:(BOOL)enabled;
- (void)On_Leave_Msg_Box;
- (void)On_Set_Vol_UI:(float)percents;
@end

@implementation PlayerViewController

@synthesize p_player_view;
@synthesize mb_view;
@synthesize enable_data_refresh_timer;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    has_rotated = FALSE;
}
*/

- (void)On_State_Change:(id)param
{
    E_PLAYER_STATE state = [Player_Model get_State];
    PLAYER_INFO info = {0};
    switch (state)
    {
        case E_PS_READY:
            p_player_view.p_pause_state_icon.hidden = YES;
            p_player_view.p_g_ps_lbl.text = @"";
            break;
        case E_PS_LOADING:
            p_player_view.p_pause_state_icon.hidden = YES;
            p_player_view.p_play_btn.state = E_PPBN_PLAY;
            p_player_view.p_g_ps_lbl.text = @"";
            break;
        case E_PS_PAUSED:
            [Player_Model Get_Info:&info];
            if (info.width <= 0 || info.height <= 0)
            {
                p_player_view.p_bg_audio_logo.hidden = NO;
            }
            else
            {
                p_player_view.p_bg_audio_logo.hidden = YES;
            }
            p_player_view.p_pause_state_icon.hidden = NO;
            p_player_view.p_play_btn.state = E_PPBN_PLAY;
            p_player_view.p_g_ps_lbl.text = IGS(EG_STR_ID_PLAYER_PAUSE);
            [self On_Stop_Control_Hide_Timer];
            break;
        case E_PS_PLAYING:
            [Player_Model Get_Info:&info];
            if (info.width <= 0 || info.height <= 0)
            {
                p_player_view.p_bg_audio_logo.hidden = NO;
            }
            else
            {
                p_player_view.p_bg_audio_logo.hidden = YES;
            }
            p_player_view.p_pause_state_icon.hidden = YES;
            p_player_view.p_play_btn.state = E_PPBN_PAUSE;
            p_player_view.p_g_ps_lbl.text = IGS(EG_STR_ID_PLAYER_PLAY);
            [self On_Fire_Control_Hide_Timer];
            break;
        case E_PS_END:
            p_player_view.p_pause_state_icon.hidden = NO;
            p_player_view.p_play_btn.state = E_PPBN_PLAY;
            p_player_view.p_g_ps_lbl.text = IGS(EG_STR_ID_PLAYER_PAUSE);
            [self On_Change_File:YES];
            break;
        case E_PS_CLOSING:
            p_player_view.p_bg_audio_logo.hidden = YES;
            p_player_view.p_pause_state_icon.hidden = YES;
            p_player_view.p_play_btn.state = E_PPBN_PLAY;
            p_player_view.p_g_ps_lbl.text = @"";
            break;
        default:
            break;
    }
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    has_rotated = FALSE;
    should_rotate = NO;
    [super viewDidLoad];
    
    CGRect rect = {0};
    CGPoint pnt = {0};
    if (get_device_type() == DVC_TYPE_IPAD)
    {
        //rect = {0};
        rect.size.width = 1024.f;
        rect.size.height = 768.f;
        self.view.bounds = rect;
        pnt.x = rect.origin.x + 0.5f * rect.size.width;
        pnt.y = rect.origin.y + 0.5f * rect.size.height;
        self.view.center = pnt;
        p_player_view.bounds = self.view.bounds;
    }
    
    //Add subviews
    [self.view addSubview:p_player_view];
    //Init Player Model
    if ([Player_Model Init])
    {
        for (int i = 0; i < E_PCB_COUNT; ++i)
        {
            [Player_Model set_CallBack:i target:self callback:@selector(On_State_Change:) withObject:self];
        }
        [Player_Model setRenderView:p_player_view];
    }
	[p_player_view create_SubViews:self];
    p_player_view.hidden = TRUE;
    [self.view addSubview:mb_view];
    [mb_view create_SubViews:self];
    mb_view.hidden = TRUE;
    
    cur_url = malloc(MAX_ICE_URL_LEN);
    
    //Some Strings
    CREATE_ICE_STRING_GLOBALIZE(leave_mb_title, ICE_String, EG_STR_ID_MB_REACH_EDGE);
    CREATE_ICE_STRING_GLOBALIZE(leave_mb_content, ICE_String, EG_STR_ID_EMPTY);
    CREATE_ICE_STRING_GLOBALIZE(error_mb_title, ICE_String, EG_STR_ID_MB_FILE_ERROR_TITLE);
    CREATE_ICE_STRING_GLOBALIZE(error_mb_content, ICE_String, EG_STR_ID_EMPTY);
    
    [leave_mb_title replaceAllString:IGS(EG_STR_ID_EMPTY)];
    [leave_mb_content replaceAllString:IGS(EG_STR_ID_MB_REACH_EDGE)];
    [error_mb_title replaceAllString:IGS(EG_STR_ID_EMPTY)];
    [error_mb_content replaceAllString:IGS(EG_STR_ID_MB_FILE_ERROR_TITLE)];
    
    data_refresh_timer = NULL;
    control_hide_timer = NULL;
    cur_play_sec = -1;
    file_len_sec = -1;
    is_timering_control_hide = NO;
    is_silented = NO;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    if (data_refresh_timer)
    {
        [data_refresh_timer invalidate];
        //[data_refresh_timer release];
        data_refresh_timer = NULL;
    }
    
    [self On_Stop_Control_Hide_Timer];
    
    // Remove all subviews from super view
    [mb_view destroy_SubViews];
    [mb_view removeFromSuperview];
    [p_player_view destroy_SubViews];
    [p_player_view removeFromSuperview];
    
    RELEASE_ICE_STRING(leave_mb_title);
    RELEASE_ICE_STRING(leave_mb_content);
    RELEASE_ICE_STRING(error_mb_title);
    RELEASE_ICE_STRING(error_mb_content);
    
    // Uninit Player Model
    [Player_Model setRenderView:NULL];
    [Player_Model Un_Init];
    free(cur_url);
    cur_url = NULL;
}


- (void)dealloc {
    [super dealloc];
}

- (void)create_SubViews:(UIViewController*)controller
{
    //add all controls here
}

- (void)destroy_SubViews
{
	//distroy all controls here
}

- (void)On_Finished_Display
{
	UIApplication* app = [UIApplication sharedApplication];
    CGRect sc_rct = [UIScreen mainScreen].bounds;
	if ((app.statusBarOrientation == UIInterfaceOrientationPortrait || app.statusBarOrientation ==UIDeviceOrientationPortraitUpsideDown))
	{
        if (get_device_type() == DVC_TYPE_IPAD)
        {
            self.view.center = CGPointMake(384.0, 512.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(M_PI * 1.f / 2.f);
            self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 768.0);
        }
        else
        {
            self.view.center = CGPointMake(160.0, 240.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.transform = CGAffineTransformMakeRotation(M_PI * 1.f / 2.f);
            self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 320.0);
        }
        has_rotated = TRUE;
	}
    else
    {
        if (get_device_type() == DVC_TYPE_IPAD)
        {
            self.view.center = CGPointMake(512.0, 384.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 768.0);
        }
        else
        {
            self.view.center = CGPointMake(240.0, 160.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 320.0);
        }
        has_rotated = FALSE;
    }
    p_player_view.hidden = FALSE;
    [self On_Set_Vol_UI:[Player_Model get_Volume]];
    [p_player_view layoutSubviews];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || 
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        return NO;
    }
    
    if (has_rotated)
    {
        should_rotate = YES;
        has_rotated = FALSE;
        return NO;
    }
    
    return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    if (should_rotate)
    {
        if (get_device_type() == DVC_TYPE_IPAD)
        {
            self.view.center = CGPointMake(512.0, 384.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 768.0);
        }
        else
        {
            self.view.center = CGPointMake(240.0, 160.0);
            self.view.transform = CGAffineTransformIdentity;
            self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 320.0);
        }
        should_rotate = FALSE;
    }
}

-(IBAction)On_Put_Play_Btn:(id)sender
{
    //check is in play state now
    switch ([Player_Model get_State])
    {
        case E_PS_PLAYING:
            [Player_Model Pause];
            break;
        case E_PS_PAUSED:
            [Player_Model Play];
            break;
        default:
            break;
    }
}

-(IBAction)On_Put_Screen_Mode_Btn:(id)sender
{
    switch ([Player_Model get_State])
    {
        case E_PS_PLAYING:
        case E_PS_PAUSED:
            [Player_Model change_Screen_Mode];
            break;
        default:
            break;
    }
}

-(IBAction)On_Put_Volume_Btn:(id)sender
{
    float vol = 0.f;
    if (!is_silented)
    {
        is_silented = YES;
        vol_before_silent = [Player_Model get_Volume];
    }
    else
    {
        is_silented = NO;
        vol = vol_before_silent;
    }
    [self On_Set_Vol:vol];
}

-(void)Ensure_File_Len
{
    PLAYER_INFO info = {0};
    if (file_len_sec < 0)
    {
        [Player_Model Get_Info:&info];
        file_len_sec = info.file_len_sec;
    }
    if (file_len_sec <= 0)
    {
        return;
    }
}

-(void)On_Drag_Time_Pos_Bar:(float)percents
{
    if (p_player_view.p_time_pos_bar.is_dragging || p_player_view.p_time_pos_btn.is_dragging)
    {
        [self Ensure_File_Len];
        cur_play_sec = (long)(((float)file_len_sec) * percents / 100.f);
        p_player_view.p_cur_time_lbl.text = [NSString stringWithFormat:@"%i:%02i:%02i / %i:%02i:%02i",cur_play_sec/3600,(cur_play_sec%3600)/60,cur_play_sec%60,file_len_sec/3600,(file_len_sec%3600)/60,file_len_sec%60];
    }
}

-(void)On_Drag_Vol_Pos_Bar:(float)percents
{
    p_player_view.p_cur_vol_lbl.text = [NSString stringWithFormat:@"%.0f%%",percents];
    if(percents <= 0.f)
    {
        if (p_player_view.p_silent_icon.hidden)
        {
            p_player_view.p_silent_icon.hidden = NO;
        }
    }
    else
    {
        if (!p_player_view.p_silent_icon.hidden)
        {
            p_player_view.p_silent_icon.hidden = YES;
        }
    }
}

-(void)On_update_Time_Pos_Controls
{
    float percents = 0.f;
    if (!p_player_view.p_time_pos_bar.is_dragging && !p_player_view.p_time_pos_btn.is_dragging)
    {
        percents = ((float)cur_play_sec) * 100.f / ((float)file_len_sec);
        [p_player_view.p_time_pos_bar Set_Time_Pos_Bar_Pos:percents];
        p_player_view.p_cur_time_lbl.text = [NSString stringWithFormat:@"%i:%02i:%02i / %i:%02i:%02i",cur_play_sec/3600,(cur_play_sec%3600)/60,cur_play_sec%60,file_len_sec/3600,(file_len_sec%3600)/60,file_len_sec%60];
    }
}

-(void)On_set_Time_Pos:(float)percents
{
    [self Ensure_File_Len];
    cur_play_sec = (long)(((float)file_len_sec) * percents / 100.f);
    [Player_Model Seek:cur_play_sec];
    [self On_update_Time_Pos_Controls];
}

-(void)On_Set_Vol_UI:(float)percents
{
    volume = percents;
    [p_player_view.p_vol_pos_bar Set_Vol_Btn_On_Volume:percents];
    [self On_Drag_Vol_Pos_Bar:percents];
    if (percents > 0.f)
    {
        is_silented = NO;
    }
    else
    {
        is_silented = YES;
    }
}

-(void)On_Set_Vol:(float)percents
{
    volume = percents;
    percents = [Player_Model set_Volume:percents];
    [self On_Set_Vol_UI:percents];
}

-(IBAction)On_Put_Loop_Mode_Btn:(id)sender
{
    gf_get_ice_cfg()->loop_mode = (gf_get_ice_cfg()->loop_mode + 1)%E_LM_COUNT;
    E_LOOP_MODE lm = gf_get_ice_cfg()->loop_mode;
    switch (lm)
    {
        case E_LM_NO_LOOP:
            p_player_view.p_loop_mode_btn.state = E_PLM_NO_LOOP;
            [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
            p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_NO_LOOP);
            break;
        case E_LM_SINGLE_LOOP:
            p_player_view.p_loop_mode_btn.state = E_PLM_SINGLE_LOOP;
            [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
            p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_SINGLE_LOOP);
            break;
        case E_LM_LIST_LOOP:
            p_player_view.p_loop_mode_btn.state = E_PLM_LIST_LOOP;
            [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
            p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_LIST_LOOP);
            break;
        case E_LM_RANDOM:
            p_player_view.p_loop_mode_btn.state = E_PLM_RANDOM;
            [BGView get_Last_BG_view].configure_view.lbl_loop_mode_state.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
            p_player_view.p_g_lm_lbl.text = IGS(EG_STR_ID_LOOP_MODE_RANDOM);
            break;
        default:
            break;
    }
    gf_set_ice_cfg(gf_get_ice_cfg());
}

- (void)On_Display_Player_View:(char*)url
{
	g_is_in_player_now = true;
    
    //[self.view addSubview:p_player_view];
    //[p_player_view create_SubViews:self];
    p_player_view.p_cur_time_lbl.text = @"";
    
    self.view.hidden = FALSE;
	CATransition *animation = [CATransition animation];
	animation.delegate = self.view;
	animation.duration = 0.2f;
	animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;//kCAFillModeBackwards;//
	animation.type = kCATransitionPush;//@"rippleEffect";//110
	animation.subtype = kCATransitionFromRight;//kCATransitionFromTop;
	//animation.endProgress = 1.0;
	//animation.removedOnCompletion = NO;
	
	//[self.layer addAnimation:animation forKey:@"animation"];
	[self.view.layer addAnimation:animation forKey:@"animation"];
    
    strcpy(cur_url, url);
    file_idx = -1;
    
    //set file name
    char* name = url;
    int l = strlen(url);
    for (int i = l - 1; i >= 0; --i)
    {
        if (name[i] == '/' || name[i] == '\\')
        {
            name = &name[i + 1];
            break;
        }
    }
    p_player_view.p_movie_name_lbl.text = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
    
    //[self.view addSubview:p_player_view];
    if(g_ice_cfg)
    {
        g_ice_cfg->enable_subtitle ? [Player_Model hide_Subtitle:NO] : [Player_Model hide_Subtitle:YES];
    }
    [Player_Model Load:cur_url];
	
	[NSTimer scheduledTimerWithTimeInterval:0.25f target:self selector:@selector(On_Finished_Display) userInfo:nil repeats:NO];
}

- (void)On_Hide_Player_View
{
    [self On_Stop_Control_Hide_Timer];
    
    p_player_view.hidden = TRUE;
	//[self On_Hide_Controls];
	CATransition *animation = [CATransition animation];
	animation.delegate = self.view;
	animation.duration = 0.3f;
	animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeRemoved;//kCAFillModeBackwards;//
	animation.type = kCATransitionPush;//@"rippleEffect";//110
	animation.subtype = kCATransitionFromLeft;//kCATransitionFromBottom;//
	//animation.endProgress = 1.0;
	//animation.removedOnCompletion = NO;
	
	//[self.layer addAnimation:animation forKey:@"animation"];
	[self.view.layer addAnimation:animation forKey:@"animation"];
	self.view.hidden = TRUE;
    g_is_in_player_now = false;
	
    [Player_Model Close];
    
	//[NSTimer scheduledTimerWithTimeInterval:0.4f target:self selector:@selector(On_Display_Controls) userInfo:nil repeats:NO];
}

- (void)File_Exchange
{
    if (![Player_Model Close])
    {
        [NSTimer scheduledTimerWithTimeInterval:0.005f target:self selector:@selector(File_Exchange) userInfo:nil repeats:NO];
    }
    else
    {
        if(g_ice_cfg)
        {
            g_ice_cfg->enable_subtitle ? [Player_Model hide_Subtitle:NO] : [Player_Model hide_Subtitle:YES];
        }
        //set file name
        {
            char* name = cur_url;
            int l = strlen(cur_url);
            for (int i = l - 1; i >= 0; --i)
            {
                if (name[i] == '/' || name[i] == '\\')
                {
                    name = &name[i + 1];
                    break;
                }
            }
            p_player_view.p_movie_name_lbl.text = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
        }
        [Player_Model Load:cur_url];
    }
}

- (void)On_Leave_Player
{
    [[BGView get_Last_BG_view] On_Leave_Player_View:0];
}

- (void)On_Call_Msg_Box:(ICE_String*)title msg:(ICE_String*)content should_leave:(BOOL)enabled
{
    if (mb_view)
    {
        [p_player_view Enable_All_Control:NO];
        p_player_view.userInteractionEnabled = NO;
        msg_box_leave = enabled;
        [mb_view Show_Box:MT_SINGLE_BTN_BOX title_content:title msg_content:content parent_view:self.view result_obj:self result_notify:@selector(On_Leave_Msg_Box)];
    }
}

- (void)On_Leave_Msg_Box
{
    p_player_view.userInteractionEnabled = YES;
    [p_player_view Enable_All_Control:YES];
    if (msg_box_leave)
    {
        [self On_Leave_Player];
    }
}

- (BOOL)On_Change_File:(BOOL)is_forward
{
    int i = 0;
    char* cur_name = NULL;
    BOOL should_change = YES;
    long t_idx = 0;
    FILE_TYPE ft = 0;
    for (i = strlen(cur_url) - 1 ; i >= 0; --i)
    {
        if (cur_url[i] == '/' || cur_url[i] == '\\')
        {
            cur_name = &cur_url[i + 1];
            break;
        }
    }
    if (file_idx < 0)
    {
        for (i = 0; i < g_current_file_list->count; ++i)
        {
            if (strcmp(cur_name, g_current_file_list->array[i].file_name) == 0)
            {
                file_idx = i;
                break;
            }
        }
    }
    switch (gf_get_ice_cfg()->loop_mode)
    {
        case E_LM_NO_LOOP:
            t_idx = file_idx;
            if (is_forward)
            {
                t_idx++;
                while (t_idx < g_current_file_list->count)
                {
                    ft = get_file_type_by_file_name(g_current_file_list->array[t_idx].file_name);
                    if (ft == F_T_VIDEO || ft == F_T_AUDIO)
                    {
                        break;
                    }
                    t_idx++;
                }
                file_idx = t_idx;
                if (file_idx >= (g_current_file_list->count))
                {
                    [Player_Model Pause];
                    [self On_Call_Msg_Box:leave_mb_title msg:leave_mb_content should_leave:NO];
                    should_change = NO;
                    break;
                }
            }
            else
            {
                t_idx--;
                while (t_idx >= 0)
                {
                    ft = get_file_type_by_file_name(g_current_file_list->array[t_idx].file_name);
                    if (ft == F_T_VIDEO || ft == F_T_AUDIO)
                    {
                        break;
                    }
                    t_idx--;
                }
                file_idx = t_idx;
                if (file_idx < 0)
                {
                    [Player_Model Pause];
                    [self On_Call_Msg_Box:leave_mb_title msg:leave_mb_content should_leave:NO];
                    file_idx = 0;
                    should_change = NO;
                    break;
                }
            }
            strcpy(cur_name, g_current_file_list->array[file_idx].file_name);
            break;
        case E_LM_LIST_LOOP:
            t_idx = 2 * g_current_file_list->count;
            while (t_idx >= 0)
            {
                if (is_forward)
                {
                    file_idx++;
                }
                else
                {
                    file_idx--;
                }
                file_idx = (file_idx + g_current_file_list->count) % g_current_file_list->count;
                ft = get_file_type_by_file_name(g_current_file_list->array[file_idx].file_name);
                if (ft == F_T_VIDEO || ft == F_T_AUDIO)
                {
                    break;
                }
                --t_idx;
            }
            strcpy(cur_name, g_current_file_list->array[file_idx].file_name);
            break;
        case E_LM_SINGLE_LOOP:
            break;
        case E_LM_RANDOM:
            srand(time(NULL));
            t_idx = 10 * g_current_file_list->count;
            while (t_idx >= 0)
            {
                file_idx = rand()%g_current_file_list->count;
                ft = get_file_type_by_file_name(g_current_file_list->array[file_idx].file_name);
                if (ft == F_T_VIDEO || ft == F_T_AUDIO)
                {
                    break;
                }
                --t_idx;
            }
            ft = get_file_type_by_file_name(g_current_file_list->array[file_idx].file_name);
            if (ft == F_T_VIDEO || ft == F_T_AUDIO)
            {
                strcpy(cur_name, g_current_file_list->array[file_idx].file_name);
            }
            else
            {
                should_change = NO;
            }
            break;
        default:
            break;
    }
    if (should_change)
    {
        [self File_Exchange];
        return YES;
    }
    return NO;
}

- (void)On_Refresh_Data
{
    PLAYER_INFO info;
    [Player_Model Get_Info:&info];
    BOOL should_update_time_pos = NO;
    E_PLAYER_STATE state = [Player_Model get_State];
    switch (state)
    {
        case E_PS_NONE:
        case E_PS_READY:
        case E_PS_LOADING:
        case E_PS_CLOSING:
            p_player_view.p_cur_time_lbl.text = @"";
            break;
        case E_PS_PLAYING:
        case E_PS_PAUSED:
            if (file_len_sec != info.file_len_sec)
            {
                file_len_sec = info.file_len_sec;
                should_update_time_pos = YES;
            }
            if (cur_play_sec != info.cur_play_sec)
            {
                cur_play_sec = info.cur_play_sec;
                should_update_time_pos = YES;
            }
            if (should_update_time_pos)
            {
                [self On_update_Time_Pos_Controls];
            }
            break;
    }
    if (volume != info.volume)
    {
        [self On_Drag_Vol_Pos_Bar:info.volume];
        [self On_Set_Vol:info.volume];
    }
}

- (void)setEnable_data_refresh_timer:(BOOL)enable
{
    if (enable == enable_data_refresh_timer)
    {
        return;
    }
    
    if (enable)
    {
        if (data_refresh_timer)
        {
            [data_refresh_timer invalidate];
            //[data_refresh_timer release];
            data_refresh_timer = NULL;
        }
        [self On_Refresh_Data];
        data_refresh_timer = [NSTimer scheduledTimerWithTimeInterval:0.25f target:self selector:@selector(On_Refresh_Data) userInfo:nil repeats:YES];
    }
    else
    {
        [data_refresh_timer invalidate];
        //[data_refresh_timer release];
        data_refresh_timer = NULL;
    }
    enable_data_refresh_timer = enable;
}

#define VIDEO_CHECK_X 0.3
#define VIDEO_CHECK_Y 0.25

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("B");
    touch_event_has_posted = NO;
    CGPoint pt = [[touches anyObject] locationInView:self.view];
    CGRect bnds = self.view.bounds;
    [self On_Stop_Control_Hide_Timer];
    if (p_player_view.is_tvout_running)
    {
        [p_player_view On_Display_All_Control];
        if (pt.x > bnds.size.width * VIDEO_CHECK_X && pt.x < bnds.size.width * (1.f - VIDEO_CHECK_X) && pt.x > bnds.size.height * VIDEO_CHECK_Y && pt.x < bnds.size.height * (1.f - VIDEO_CHECK_Y))
        {
            touch_event_has_posted = YES;
            [p_player_view touchesBegan:touches withEvent:event];
        }
    }
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("M");
    if (touch_event_has_posted)
    {
        [p_player_view touchesMoved:touches withEvent:event];
    }
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("C");
    if ([Player_Model get_State] == E_PS_PLAYING || [Player_Model get_State] == E_PS_PAUSED)
    {
        [self On_Fire_Control_Hide_Timer];
    }
    if (touch_event_has_posted)
    {
        touch_event_has_posted = NO;
        [p_player_view touchesCancelled:touches withEvent:event];
    }
    [super touchesCancelled:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //printf("E");
    if ([Player_Model get_State] == E_PS_PLAYING || [Player_Model get_State] == E_PS_PAUSED)
    {
        [self On_Fire_Control_Hide_Timer];
    }
    if (touch_event_has_posted)
    {
        touch_event_has_posted = NO;
        [p_player_view touchesEnded:touches withEvent:event];
    }
    [super touchesEnded:touches withEvent:event];
}

- (void)On_Stop_Control_Hide_Timer
{
    if (control_hide_timer)
    {
        if (is_timering_control_hide)
        {
            int count = [control_hide_timer retainCount];
            [control_hide_timer invalidate];
        }
        is_timering_control_hide = NO;
        //[control_hide_timer release];
        control_hide_timer = NULL;
    }
}

- (void)On_Fire_Control_Hide_Timer
{
    [self On_Stop_Control_Hide_Timer];
    is_timering_control_hide = YES;
    control_hide_timer = [NSTimer scheduledTimerWithTimeInterval:6.f target:self selector:@selector(On_Do_Hide_Control) userInfo:nil repeats:NO];
}

- (void)On_Do_Hide_Control
{
    [p_player_view On_Hide_All_Control];
    is_timering_control_hide = NO;
}

@end
